package com.mall.server.service.sms;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.autoconfig.exception.BusinessException;
import com.mall.dubbo.api.SmsHomeNewProductApi;
import com.mall.model.domain.SmsHomeNewProduct;
import com.mall.model.vo.CommonPage;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;

@Service
public class SmsHomeNewProductService {

    @DubboReference
    private SmsHomeNewProductApi smsHomeNewProductApi;

    //分页查询首页新品
    public CommonPage list(Integer pageNum, Integer pageSize, SmsHomeNewProduct newProduct) {

        String productName = newProduct.getProductName();
        Integer recommendStatus = newProduct.getRecommendStatus();

        Page<SmsHomeNewProduct> page = smsHomeNewProductApi.listPage(pageNum, pageSize, productName, recommendStatus);

        return new CommonPage(page);
    }

    //添加首页新品
    public void create(List<SmsHomeNewProduct> homeNewProductList) {

        for (SmsHomeNewProduct homeNewProduct : homeNewProductList) {
            Random r = new Random();
            int sort = r.nextInt(100) + 50;
            homeNewProduct.setSort(sort);
            homeNewProduct.setRecommendStatus(1);
        }

        smsHomeNewProductApi.saveBatch(homeNewProductList);

    }

    //批量删除首页新品
    public void delete(List<Long> ids) {

        smsHomeNewProductApi.removeByIds(ids);
    }

    //批量修改首页新品状态
    public void update(List<Long> ids, SmsHomeNewProduct homeNewProduct) {

        List<SmsHomeNewProduct> list = smsHomeNewProductApi.findByIds(ids, homeNewProduct);

        if (!CollUtil.isEmpty(list)) {
            throw new BusinessException("这是错误的操作");
        }
        smsHomeNewProductApi.updateByIds(ids, homeNewProduct);
    }


    //修改首页新品排序
    public void updateSort(Integer id, SmsHomeNewProduct homeNewProduct) {

        smsHomeNewProductApi.updateSort(id, homeNewProduct);
    }

}
